package com.hddf.project.interceptor;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.kommii.log.LogWriter;

import com.hddf.project.pojos.User;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;

public class LogInterceptor extends AbstractInterceptor {

	private static final long serialVersionUID = 565445990123435L;

	@Override
	public synchronized String intercept(ActionInvocation invocation) throws Exception {
		String res = invocation.invoke();
		HttpServletRequest request = null;
		request = (HttpServletRequest) invocation.getInvocationContext().get(ServletActionContext.HTTP_REQUEST);

		User u = (User) request.getAttribute("user");
		if (null != u) {
			String uname = "uname: " + u.getUsername() + " ,userid: " + u.getId();
			String uri = (String) request.getAttribute("uri");
			uname = uname + " ,action :" + uri;
			String logname = (String) request.getAttribute("logname");
			String logs = (String) request.getAttribute("logs");
			if (null != logname) {
				LogWriter.writeLog(logname, uname, logs);
			}
		}
		return res;
	}
}